package durak.server.control;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import durak.common.model.Card;
import durak.common.model.Card.Rank;
import durak.common.model.Card.Suit;
import durak.common.model.DurakDeck;
import durak.server.model.Player;
import durak.server.model.Player.playerType;

public class ReadFromXmlFile {

	Card card;
	Element eElement;
	ArrayList<Player> players;
	DurakDeck deck;
	String playerName;
	String suit;
	String rank;
	Player player;

	File xmlFile = new File("c:\\durak.xml");
	File xsdFile = new File("c:\\durak.xds");

	public boolean validateXml(String xmlFile, String xsdFile) throws SAXException, IOException {
		File fSchemaFile = new File(xsdFile);
		Source XmlFile = new StreamSource(xmlFile);

		SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = schemaFactory.newSchema(fSchemaFile);
		Validator validator = schema.newValidator();

		try {
			validator.validate(XmlFile);
			System.out.println(XmlFile.getSystemId() + " is valid");
			return true;

		} catch (SAXException e) {
			System.out.println(XmlFile.getSystemId() + " is NOT valid");
			System.out.println("Reason: " + e.getLocalizedMessage());
			return false;
		}

	}

	public void parseXmlFile(String xmlPath) throws Throwable {

		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(xmlFile); // doc holds the file
		doc.getDocumentElement().normalize();

		players = new ArrayList<Player>();
		NodeList playersList = doc.getElementsByTagName("player"); // 2 players

		for (int i = 0; i < playersList.getLength(); i++) {
			Node fstNode = playersList.item(i);// player

			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element fstElmnt = (Element) fstNode;
				String type = fstElmnt.getAttribute("type");

				if (type.equals("HUMAN")) {
					playerName = fstElmnt.getAttribute("name");
					player = new Player();// creating a new human player
					player.setName(playerName);
					player.setType(Player.playerType.HUMAN);
					players.add(player);
				}

				else if (type.equals("COMPUTER")) {
					playerName = fstElmnt.getAttribute("name");
					player = new Player();// creating a new human player
					player.setName(playerName);
					player.setType(Player.playerType.COMPUTER);
					players.add(player);

				}

			}
		}

		deck = new DurakDeck();
		NodeList cardsList = doc.getElementsByTagName("card");

		for (int i = 0; i < cardsList.getLength(); i++) {
			Node fstNode = cardsList.item(i);// card
			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
				Element fstElmnt = (Element) fstNode;
				String suit = fstElmnt.getAttribute("suit");// "DIAMONDS"
				String rank = fstElmnt.getAttribute("rank");// "ACE"

				if (suit.equals("DIAMONDS") && rank.equals("SIX")) {
					card = new Card(Rank.SIX, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("QUEEN")) {
					card = new Card(Rank.QUEEN, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("KING")) {
					card = new Card(Rank.KING, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("KING"))//
				{
					card = new Card(Rank.KING, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("SIX")) {
					card = new Card(Rank.SIX, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("SIX")) {
					card = new Card(Rank.SIX, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("SIX")) {
					card = new Card(Rank.SIX, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("TEN")) {
					card = new Card(Rank.TEN, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("SEVEN")) {
					card = new Card(Rank.SEVEN, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("TEN")) {
					card = new Card(Rank.TEN, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("JACK")) {
					card = new Card(Rank.JACK, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("JACK")) {
					card = new Card(Rank.JACK, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("ACE")) {
					card = new Card(Rank.ACE, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("EIGHT")) {
					card = new Card(Rank.EIGHT, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("NINE")) {
					card = new Card(Rank.NINE, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("QUEEN")) {
					card = new Card(Rank.QUEEN, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("KING")) {
					card = new Card(Rank.KING, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("TEN")) {
					card = new Card(Rank.TEN, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("EIGHT")) {
					card = new Card(Rank.EIGHT, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("TEN")) {
					card = new Card(Rank.TEN, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("ACE")) {
					card = new Card(Rank.ACE, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("EIGHT")) {
					card = new Card(Rank.EIGHT, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("SEVEN")) {

					card = new Card(Rank.SEVEN, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("SEVEN")) {
					card = new Card(Rank.SEVEN, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("NINE")) {
					card = new Card(Rank.NINE, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("SEVEN")) {
					card = new Card(Rank.SEVEN, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("SPADES") && rank.equals("JACK")) {

					card = new Card(Rank.JACK, Suit.SPADES);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("ACE")) {
					card = new Card(Rank.ACE, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("QUEEN")) {
					card = new Card(Rank.QUEEN, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("KING")) {
					card = new Card(Rank.KING, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("JACK")) {
					card = new Card(Rank.JACK, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("ACE")) {
					card = new Card(Rank.ACE, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("EIGHT")) {
					card = new Card(Rank.EIGHT, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("HEARTS") && rank.equals("QUEEN")) {
					card = new Card(Rank.QUEEN, Suit.HEARTS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("SEVEN")) {
					card = new Card(Rank.SEVEN, Suit.CLUBS);
					deck.addCard(card);
				}
				if (suit.equals("DIAMONDS") && rank.equals("NINE")) {
					card = new Card(Rank.NINE, Suit.DIAMONDS);
					deck.addCard(card);
				}
				if (suit.equals("CLUBS") && rank.equals("NINE")) {
					card = new Card(Rank.NINE, Suit.CLUBS);
					deck.addCard(card);
				}

			}

		}

		print(deck, players);
	}

	public void print(DurakDeck deck, ArrayList<Player> p) {
		for (int i = 0; i < deck.size(); i++) {
			System.out.println(deck.getCardByLocation(i));
		}

		for (int i = 0; i < p.size(); i++) {
			System.out.println(p.get(i).getName() + " - " + p.get(i).getType());
		}
	}

	public DurakDeck returnDeck() {
		return deck;
	}

	public ArrayList<Player> returnPlayers1() {
		return players;
	}
	
	public ArrayList<Player> returnOnlyHuman()
	{
		ArrayList<Player> onlyHuman = new ArrayList<Player>();
		for ( int i =0 ; i < players.size() ; i++)
		{
			if ( players.get(i).getType().compareTo(playerType.HUMAN)== 0)
				onlyHuman.add(players.get(i));
		}
		return onlyHuman;
	}
	
	public ArrayList<Player> returnOnlyAuto()
	{
		ArrayList<Player> onlyAuto = new ArrayList<Player>();
		for ( int i =0 ; i < players.size() ; i++)
		{
			if ( players.get(i).getType().compareTo(playerType.COMPUTER)== 0)
				onlyAuto.add(players.get(i));
		}
		return onlyAuto;
	}
	

	public int numOfHumanPalyers() {
		return returnOnlyHuman().size();
	}

	public int numOfAutoPalyers() {
		return returnOnlyAuto().size();
	}

}
